<?php include_once('includes/header.php'); ?>
<?php include_once('scripts/get-categories.php'); ?>
<?php include_once('functions/posts.php'); ?>
<?php
	if (isset($_GET['id'])) {
		$post_id = $_GET['id'];
		$page_title = "Edit Post";
		//Get the post
		$post = get_post_by_id($post_id);
		extract($post);
	} 
	else {
		$page_title = "Create Post";
		$post_id = 0;
		$title = '';
		$content = '';
		$cat_id = -1;
		$state = -1;
		$post_meta = '';
		$post_robots = '';
		$meta_title = '';
		$meta_description = '';
	}
?>
		<script type="text/javascript">
		   $(document).ready(function() {
		      $("#markItUp").markItUp(mySettings);
		   });
		</script>
		<script type="text/javascript" src="js/categories.js"></script>
		<script type="text/javascript" src="js/create-posts.js"></script>
		<?php include_once('includes/posts-header.php') ?>
		<div class="col-full">
			<form  method="post" action="create-posts.php" id="postform" name="postform">
				<div id="toolbar" class="floatright">
					<ul>
						<li><a href="#" class="toolbar" id="save"><span class="save"></span>Save</a></li>
						<li><a href="#" class="toolbar" id="save-close"><span class="save-close"></span>Save &amp; Close</a></li>
						<?php if ($post_id  == 0) { ?>
						<li><a href="#" class="toolbar" id="save-new"><span class="save-new"></span>Save &amp; New</a></li>
						<?php } ?>
						<li><a href="#" class="toolbar" id="cancel"><span class="cancel"></span>Cancel</a></li>
					</ul>
				</div><!-- End #toolbar-->
				<div id="error-message"></div>
				<div id="leftsection" class="floatleft">
					<h2><?php echo $page_title; ?></h2>
					<div class="form-elements">
						<label for="title">Title</label>
						<input placeholder="Add Title Here" type="text" name="title" id="title" value = "<?php echo $title; ?>" />
					</div><!-- End .form-elements-->
					<div class="form-elements">
						<label for="category">Category</label>
						<?php create_categories_list("categoryname",$cat_id);?>
					</div><!-- End .form-elements-->
					<div class="form-elements">
						<label for="state">State</label>
						<select name="state" id="state">
							<option <?php if ($state == 1) echo 'selected="selected"'; ?> value="1">Publish</option>
							<option <?php if ($state == 2) echo 'selected="selected"'; ?> value="2">Unpublish</option>
							<option <?php if ($state == 3) echo 'selected="selected"'; ?> value="3">Trash</option>
						</select>
					</div><!-- End .form-elements-->
					<div class="form-elements">
						<label for="content">Content</label>
						<textarea name="content" id="markItUp"><?php echo $content; ?></textarea>
					</div><!-- End .form-elements-->
				</div><!-- End #leftsection-->
				<div id="rightsection" class="floatright">
					<h2>Metadata</h2>
					<div class="form-elements">
						<label for="metatags">Meta Tags</label>
						<input type="text" name="post_meta" id="post_meta" placeholder="Meta Tags" value = "<?php echo $post_meta; ?>" />
					</div><!-- End .form-elements-->
					<div class="form-elements">
						<label for="metaRobots">Meta Robots</label>
						<input type="text" name="post_robots" id="post_robots" placeholder="Meta Robots" value = "<?php echo $post_robots; ?>" />
					</div><!-- End .form-elements-->
					<div class="form-elements">
						<label for="metatitle">Meta Title</label>
						<input type="text" name="meta_title" id="meta_title" placeholder="Meta Title" value = "<?php echo $meta_title; ?>" />
					</div><!-- End .form-elements-->
					<div class="form-elements">
						<label for="metadescription">Meta Description</label>
						<input type="text" name="meta_description" id="meta_description" placeholder="Meta Description" value = "<?php echo $meta_description; ?>" />
					</div><!-- End .form-elements-->
				</div><!-- End #rightsection-->
				<div id="rightsection" class="floatright">
					<h2 id="cat-header">Create new category</h2>
					<div class="form-elements">
						<label for="cat-name">Category name</label>
						<input type="text" name="cat-name" id="cat-name" />
					</div><!-- End .form-elements-->
					<div class="form-elements">
						<label for="description">Description</label>
						<textarea name="cat-description" id="cat-description"></textarea>
					</div><!-- End .form-elements-->
					<div class="form-elements">
						<label for="state">State</label>
						<select name="new_state" id="new_state">
							<option value="1">Publish</option>
							<option value="2">Unpublish</option>
							<option value="3">Trash</option>
						</select>
					</div><!-- End .form-elements-->
					<div class="form-elements">
						<label for="category">Parent</label>
						<?php create_categories_list("parentid"); ?>
					</div><!-- End .form-elements-->
					<div class="form-elements">
						<input type="button" id="addNewCat" class="myButton" value="Add new" onclick="add_new_cat();" />
					</div><!-- End .form-elements-->
					<input type="hidden" name="cat_id" id="cat_id" value="0" />
				</div><!-- End #rightsection-->
				<input type="hidden" name="post_id" id="post_id" value="<?php echo $post_id; ?>" />
				<input type="hidden" name="type" id="type" value="0" />
			</form>
			<div class="clear"></div><!-- End .clear-->
		</div><!-- End .col-full-->
	</div><!-- End #mainWrapper-->
</body>
</html>